from rest_framework.views import APIView

# Create your views here.

from rest_framework.response import Response
from myapp.models import Exchange, VirtualCoin, KlineMfifteen, KlineMone, TrendHistory, DealValue
from myapp.serializer import ExchangeSerializer, VirtualSerializer, KlineMfifteenSer, klineMSer, TrendHistorySer


# 交易所  获取
class ExchangeView(APIView):
    # def post(self, request):
    #     exchange_name = request.data.get("exchange_name")
    #     exchange_address = request.data.get("exchange_address")
    #     exchange_code = request.data.get("exchange_code")
    #     exchange_num_all = request.data.get("exchange_num_all")
    #     if not ([exchange_name, exchange_address, exchange_code, exchange_num_all]):
    #         return Response({
    #             'code': 400,
    #             'msg': '交易所信息不完整'
    #         })
    #     ex_judge = Exchange.objects.filter(exchange_name=exchange_name).first()
    #     if ex_judge:
    #         return Response({
    #             'code': 400,
    #             'msg': '交易所已存在'
    #         })
    #     exchange = Exchange.objects.create(
    #         exchange_name=exchange_name,
    #         exchange_address=exchange_address,
    #         exchange_code=exchange_code,
    #         exchange_num_all=exchange_num_all
    #     )
    #     exchange.save()
    #     return Response({
    #         'code': 200,
    #         'msg': '添加交易所成功'
    #     })

    def get(self, request):
        exchange = Exchange.objects.all()
        ser = ExchangeSerializer(exchange,many=True)
        return Response({
            'code': 200,
            'msg': '获取交易所信息成功',
            'data': ser.data
        })
# 获取单个
class ExchangeOne(APIView):
    def get(self, request,pk):
        bite = VirtualCoin.objects.filter(id=pk).first()
        ser = VirtualSerializer(bite)
        return Response({
            'code': 200,
            'msg': '获取单个数据成功',
            'data': ser.data
        })


# 币 获取
class VirtualCoinView(APIView):
    # def post(self, request):
    #     coin_name = request.data.get("coin_name")
    #     coin_acronym = request.data.get("coin_acronym")
    #     coin_icon = request.data.get("coin_icon")
    #     coin_rank_list = request.data.get("coin_rank_list")
    #     coin_now_price = request.data.get("coin_now_price")
    #     coin_value = request.data.get("coin_value")
    #     coin_oneday_turnover = request.data.get("coin_oneday_turnover")
    #     coin_lowest = request.data.get("coin_lowest")
    #     coin_highest = request.data.get("coin_highest")
    #     if not ([coin_name, coin_acronym, coin_icon, coin_rank_list, coin_now_price, coin_value,coin_oneday_turnover, coin_lowest, coin_highest]):
    #         return Response({
    #             'code': 400,
    #             'msg': '请完善币种信息',
    #         })
    #     bite = VirtualCoin.objects.filter(coin_name=coin_name).first()
    #     if bite:
    #         return Response({
    #             'code': 400,
    #             'msg': '该币种已存在'
    #         })
    #     icon_bite = VirtualCoin.objects.create(
    #         coin_name=coin_name,
    #         coin_acronym=coin_acronym,
    #         coin_icon=coin_icon,
    #         coin_rank_list=coin_rank_list,
    #         coin_now_price=coin_now_price,
    #         coin_value=coin_value,
    #         coin_lowest=coin_lowest,
    #         coin_highest=coin_highest
    #     )
    #     icon_bite.save()
    #     return Response({
    #         'code': 200,
    #         'msg': '币种添加成功'
    #     })

    def get(self, request):
        coin_info = VirtualCoin.objects.all()
        ser = VirtualSerializer(coin_info,many=True)
        return Response({
                'code': 200,
                'msg': '币种获取成功',
                'data': ser.data,
            })
# 获取单个
class OneIcon(APIView):
    def get(self, request,pk):
        bite = VirtualCoin.objects.filter(id=pk).first()
        ser = VirtualSerializer(bite)
        return Response({
            'code': 200,
            'msg': '获取单个数据成功',
            'data': ser.data
        })


# 交易对现价 获取
class TradingView(APIView):
    def get(self, request):
        trading = DealValue.objects.all()
        ser = DealValueSer(trading,many=True)
        return Response({
            'code': 200,
            'msg': '交易对获取成功',
            'data': ser.data,
        })
# 获取单个交易对
class OneTrading(APIView):
    def get(self, request):
        id = request.data.get("id")
        trading = DealValue.objects.filter(id=id).first()
        ser = DealValueSer(trading)
        return Response({
            'code': 200,
            'msg': '获取单个数据成功',
            'data': ser.data
        })


# 币历史数据 获取
class HistoryView(APIView):
    def get(self, request):
        history = TrendHistory.objects.all()
        ser = TrendHistorySer(history,many=True)
        return Response({
            'code': 200,
            'msg': '历史获取成功',
            'data': ser.data,
        })
# 获取单个历史
class OneHistory(APIView):
    def get(self, request):
        id = request.data.get("id")
        history = TrendHistory.objects.filter(id=id).first()
        ser = TrendHistorySer(history)
        return Response({
            'code': 200,
            'msg': '获取单个数据成功',
            'data': ser.data
        })


# K线 M1  获取
class KlineView(APIView):
    def get(self, request):
        kline = KlineMone.objects.all()
        ser = klineMSer(kline,many=True)
        return Response({
            'code': 200,
            'msg': 'K线获取成功',
            'data': ser.data,
        })
# 获取单个
class OneKline(APIView):
    def get(self, request):
        id = request.data.get("id")
        kline = KlineMone.objects.filter(id=id).first()
        ser = klineMSer(kline)
        return Response({
            'code': 200,
            'msg': '获取单个数据成功',
            'data': ser.data
        })


# K线 M15  获取
class KlinefifteenView(APIView):
    def get(self, request):
        k_line_fifteen = KlineMfifteen.objects.all()
        ser = KlineMfifteenSer(k_line_fifteen,many=True)
        return Response({
            'code': 200,
            'msg': 'K线历史获取成功',
            'data': ser.data,
        })
# 获取单个
class OneKlinefifteen(APIView):
    def get(self, request):
        id = request.data.get("id")
        k_line_fifteen = KlineMfifteen.objects.filter(id=id).first()
        ser = KlineMfifteenSer(k_line_fifteen)
        return Response({
            'code': 200,
            'msg': '获取单个数据成功',
            'data': ser.data
        })